import qs from 'qs'
export const eventMixins = {
    data() {
        return {
            isShowTagsNoticeBar: false,
            customerTagArr: []

        }
    },
    mounted() {


    },

    methods: {



        fcmbjeonchange(total) {
            console.log(total)

            if (!(/^-?\d+(\.\d+)?$/.test(total))) {
                return
            }
            const average = (Number(total) / 4).toFixed(2)


            if (this.jsondata.bodydata) {
                for (let bodydatakey in this.jsondata.bodydata) {
                    if (bodydatakey !== '代理协议发出目标明细') {
                        continue
                    }
                    let bodydata = this.jsondata.bodydata[bodydatakey]

                    for (let i = 0; i < bodydata.length; i++) {
                        for (let j = 0; j < bodydata[i].length; j++) {
                            const fitem = bodydata[i][j]
                            if (fitem.sField.indexOf('yeartgmb') !== -1) {
                                fitem.value = average
                            }

                        }
                    }
                }
            }

        },

        tgmbjeonchange(total) {

            if (!(/^-?\d+(\.\d+)?$/.test(total))) {
                return
            }
            const average = (Number(total) / 4).toFixed(2)


            if (this.jsondata.bodydata) {
                for (let bodydatakey in this.jsondata.bodydata) {
                    if (bodydatakey !== '代理协议推广目标明细') {
                        continue
                    }
                    let bodydata = this.jsondata.bodydata[bodydatakey]

                    for (let i = 0; i < bodydata.length; i++) {
                        for (let j = 0; j < bodydata[i].length; j++) {
                            const fitem = bodydata[i][j]
                            if (fitem.sField.indexOf('yeartgmb') !== -1) {
                                fitem.value = average
                            }
                        }
                    }
                }
            }

        },

        // 

        tagDisplay(itemdata) {


            const data = {
                [`${itemdata.idObject}_account`]: itemdata.valuehidden
            }
            return this.axios({
                method: 'post',
                url: '/api/' + itemdata.idObject + '.lslevellist.do',
                data: qs.stringify(data)
            })

        },

        handleTagDisplayRes(res) {
            if (res.data.flag === '0') {
                // console.log(res)
                const jsondata = res.data.jsondata
                const { bqdata, qxdata, xzdata, bqdata_format, qxdata_format, xzdata_format } = jsondata
                const customerTagArr = []


                if (Array.isArray(bqdata) && bqdata.length > 0) {

                    customerTagArr.push(bqdata_format)
                }

                if (Array.isArray(qxdata) && qxdata.length > 0) {

                    customerTagArr.push(qxdata_format)

                }

                if (Array.isArray(xzdata) && xzdata.length > 0) {

                    customerTagArr.push(xzdata_format)

                }
                return customerTagArr



            }

        },

        fillTitle(jsondata, itemdata, newval) {
            console.log('填充标题事件触发', itemdata, newval, jsondata)
            const headData = jsondata.head || [];
            let title = null;
            headData.forEach(group => {
            group.groupvalue.forEach(item => {
                if (item.sField === 'bt') {
                    title = item;
                    title._global_jxssqb_bt = title._global_jxssqb_bt || title.value
                    }
                });
            });
            const regex = /\$\{([^}]+)\}/;
            const match = title._global_jxssqb_bt.match(regex);
            if (match && match[1] === 'province') {
                const values = jsondata.bodydata[this.indexName].map(item => {
                    const temp = item.filter(ite => ite.sField === 'sheng').map(ite => ite.value).filter(value => value)
                    return temp[0] || '';
                }).filter(value => value)
                const uniqueValues = [...new Set(values)];
                const replaceValue = uniqueValues.join(';');
                if (!replaceValue) {
                    title.value = title._global_jxssqb_bt;
                } else {
                    const _local_jxssqb_bt = title._global_jxssqb_bt.replace(match[0], replaceValue)
                    title.value = _local_jxssqb_bt;
                }
            }
        },

        fillTitle2(jsondata, itemdata, newval) {
            console.log('填充标题事件触发', itemdata, newval, jsondata)
            const headData = jsondata.head || [];
            let title = null;
            headData.forEach(group => {
            group.groupvalue.forEach(item => {
                if (item.sField === 'sname') {
                    title = item;
                    title._global_target_ = title._global_target_ || title.value
                    }
                });
            });
            const regex = /\$\{([^}]+)\}/;
            const match = title._global_target_.match(regex);
            if (match && match[1] === 'province') {
                const values = jsondata.head.map(item => {
                    const temp = item.groupvalue.filter(ite => ite.sField === 'sheng').map(ite => ite.value).filter(value => value)
                    return temp[0] || '';
                }).filter(value => value)
                const uniqueValues = [...new Set(values)];
                const replaceValue = uniqueValues.join(';');
                if (!replaceValue) {
                    title.value = title._global_target_;
                } else {
                    const _local_target_ = title._global_target_.replace(match[0], replaceValue)
                    title.value = _local_target_;
                }
            }
        }



    },


}
